<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.2" />






<meta name="description" content="1. Application Cache 简介1.1. 什么是 Application CacheHTML5 引入了应用程序缓存，这意味着 web 应用可进行缓存，并可在没有因特网连接时进行访问。 应用程序缓存为应用带来三个优势：  离线浏览 - 用户可在应用离线时使用它们 速度 - 已缓存资源加载得更快 减少服务器负载 - 浏览器将只从服务器下载更新过或更改过的资源  1.2. 应用缓存原理HT">
<meta property="og:type" content="article">
<meta property="og:title" content="HTML5 之 Application Cache™">
<meta property="og:url" content="http://xiezixi.oschina.io/2018/06/01/HTML5-ApplicationCache/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="1. Application Cache 简介1.1. 什么是 Application CacheHTML5 引入了应用程序缓存，这意味着 web 应用可进行缓存，并可在没有因特网连接时进行访问。 应用程序缓存为应用带来三个优势：  离线浏览 - 用户可在应用离线时使用它们 速度 - 已缓存资源加载得更快 减少服务器负载 - 浏览器将只从服务器下载更新过或更改过的资源  1.2. 应用缓存原理HT">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://xiezixi.oschina.io/blog/images/cache1.png">
<meta property="og:image" content="http://xiezixi.oschina.io/blog/images/cache2.png">
<meta property="og:image" content="http://xiezixi.oschina.io/blog/images/cache3.png">
<meta property="og:image" content="http://xiezixi.oschina.io/blog/images/cache4.png">
<meta property="og:updated_time" content="2018-06-01T08:56:26.570Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="HTML5 之 Application Cache™">
<meta name="twitter:description" content="1. Application Cache 简介1.1. 什么是 Application CacheHTML5 引入了应用程序缓存，这意味着 web 应用可进行缓存，并可在没有因特网连接时进行访问。 应用程序缓存为应用带来三个优势：  离线浏览 - 用户可在应用离线时使用它们 速度 - 已缓存资源加载得更快 减少服务器负载 - 浏览器将只从服务器下载更新过或更改过的资源  1.2. 应用缓存原理HT">
<meta name="twitter:image" content="http://xiezixi.oschina.io/blog/images/cache1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://xiezixi.oschina.io/2018/06/01/HTML5-ApplicationCache/"/>





  <title>HTML5 之 Application Cache™ | 个人博客</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">曦瓜皮</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>


 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://xiezixi.oschina.io/blog/2018/06/01/HTML5-ApplicationCache/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="曦瓜皮">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">HTML5 之 Application Cache™</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-06-01T16:56:26+08:00">
                2018-06-01
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/HTML5/" itemprop="url" rel="index">
                    <span itemprop="name">HTML5</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1-Application-Cache-简介"><a href="#1-Application-Cache-简介" class="headerlink" title="1. Application Cache 简介"></a>1. Application Cache 简介</h2><h3 id="1-1-什么是-Application-Cache"><a href="#1-1-什么是-Application-Cache" class="headerlink" title="1.1. 什么是 Application Cache"></a>1.1. 什么是 Application Cache</h3><p>HTML5 引入了应用程序缓存，这意味着 web 应用可进行缓存，并可在没有因特网连接时进行访问。</p>
<p>应用程序缓存为应用带来三个优势：</p>
<ul>
<li><strong>离线浏览</strong> - 用户可在应用离线时使用它们</li>
<li><strong>速度</strong> - 已缓存资源加载得更快</li>
<li><strong>减少服务器负载</strong> - 浏览器将只从服务器下载更新过或更改过的资源</li>
</ul>
<h3 id="1-2-应用缓存原理"><a href="#1-2-应用缓存原理" class="headerlink" title="1.2. 应用缓存原理"></a>1.2. 应用缓存原理</h3><p>HTML5 的离线存储是基于一个 manifest 文件（缓存清单文件，后缀为 .appcache）的缓存机制（不是存储技术），通过这个文件上的清单解析离线存储资源，这些资源就会像 cookie 一样被存储了下来。之后当网络在处于离线状态时，浏览器会通过被离线存储的数据进行页面展示。</p>
<h3 id="1-3-使用方法"><a href="#1-3-使用方法" class="headerlink" title="1.3. 使用方法"></a>1.3. 使用方法</h3><ul>
<li>manifest 文件的建议的文件扩展名是：<strong>.appcache</strong></li>
<li>首先在文档的 html 标签中设置 manifest 属性，引用 manifest 文件</li>
<li>然后配置 manifest 文件，在 manifest 文件中编写离线存储的资源</li>
<li>此外，必须要在服务器端正确的配置 MIME-type。即 <strong>text/cache-manifest</strong></li>
</ul>
<h3 id="1-4-浏览器兼容性"><a href="#1-4-浏览器兼容性" class="headerlink" title="1.4. 浏览器兼容性"></a>1.4. 浏览器兼容性</h3><p><a href="https://caniuse.com/#search=Offline%20web%20applications" target="_blank" rel="noopener">Offline Web Applications 浏览器兼容性</a></p>
<h2 id="2-Manifest-文件"><a href="#2-Manifest-文件" class="headerlink" title="2. Manifest 文件"></a>2. Manifest 文件</h2><p>manifest 文件是简单的文本文件，它告知浏览器被缓存的内容（以及不缓存的内容）。</p>
<p>manifest 文件可分为三个部分：</p>
<ul>
<li><strong>CACHE MANIFEST</strong> - 在此标题下列出的文件将在首次下载后进行缓存</li>
<li><strong>NETWORK</strong> -  在此标题下列出的文件需要与服务器的连接，且不会被缓存</li>
<li><strong>FALLBACK</strong> -  在此标题下列出的文件规定当页面无法访问时的回退页面（比如 404 页面）</li>
</ul>
<h2 id="3-简单实例"><a href="#3-简单实例" class="headerlink" title="3. 简单实例"></a>3. 简单实例</h2><p>接下来，用具体实例来看一下如何使用离线缓存。</p>
<p>文件夹目录如下：</p>
<p><img src="http://xiezixi.oschina.io/blog/images/cache1.png" alt="文件夹目录"></p>
<p><strong>cache.html</strong> 代码如下：<br><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!doctype html&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">manifest</span>=<span class="string">"cache.appcache"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>应用缓存<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"stylesheet"</span> <span class="attr">href</span>=<span class="string">"cache.css"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"cache.js"</span>&gt;</span><span class="undefined"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">"cache.png"</span> <span class="attr">alt</span>=<span class="string">"cache.png"</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p><strong>cache.appcache</strong> 代码如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">CACHE MANIFEST</span><br><span class="line"># v0.0.1</span><br><span class="line">CACHE:</span><br><span class="line">/cache.css</span><br><span class="line">/cache.js</span><br><span class="line">/cache.png</span><br><span class="line"></span><br><span class="line">NETWORK:</span><br><span class="line">*</span><br></pre></td></tr></table></figure></p>
<p>manifest 属性可指向绝对网址或相对路径，但绝对网址必须与相应的网络应用同源。manifest 除了缓存 manifest.appcache 文件所指定的资源外，还必定会缓存当前的 html 页面。可以使用星号来指示所有其他资源/文件都需要因特网连接。</p>
<p>注意：这里的版本号，是我们人为规定的，而非是 manifest 自带的属性，当每一次 html 加载到 manifest 时，会对 manifest 配置文件进行脏检查，当检测到 manifest 文件被修改后，之前的缓存将会被弃用，转而去根据 manifest 文件中配置的新内容进行缓存。</p>
<p><strong>缓存查看</strong><br>使用 node 或其他方法启动服务后，访问页面之后尝试关闭网络环境，依然能够访问。</p>
<p><img src="http://xiezixi.oschina.io/blog/images/cache2.png" alt="缓存查看1"></p>
<p><img src="http://xiezixi.oschina.io/blog/images/cache3.png" alt="缓存查看2"></p>
<p><img src="http://xiezixi.oschina.io/blog/images/cache4.png" alt="缓存查看3"></p>
<h2 id="4-window-applicationCache"><a href="#4-window-applicationCache" class="headerlink" title="4. window.applicationCache"></a>4. window.applicationCache</h2><p>window.applicationCache 对象是对浏览器的应用缓存的编程访问方式。其 status 属性可用于查看缓存的当前状态。</p>
<h3 id="4-1-status-属性值"><a href="#4-1-status-属性值" class="headerlink" title="4.1. status 属性值"></a>4.1. status 属性值</h3><table>
<thead>
<tr>
<th>属性值</th>
<th>含义</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td><code>UNCACHED</code></td>
<td>无缓存， 即没有与页面相关的应用缓存</td>
</tr>
<tr>
<td>1</td>
<td><code>IDLE</code></td>
<td>闲置，即应用缓存未得到更新</td>
</tr>
<tr>
<td>2</td>
<td><code>CHECKING</code></td>
<td>检查中，即正在下载描述文件并检查更新</td>
</tr>
<tr>
<td>3</td>
<td><code>DOWNLOADING</code></td>
<td>下载中，即应用缓存正在下载描述文件中指定的资源</td>
</tr>
<tr>
<td>4</td>
<td><code>UPDATEREADY</code></td>
<td>更新完成，所有资源都已下载完毕</td>
</tr>
<tr>
<td>5</td>
<td><code>IDLE</code></td>
<td>废弃，即应用缓存的描述文件已经不存在了，因此页面无法再访问应用缓存</td>
</tr>
</tbody>
</table>
<p>以下代码使用 status 属性为当前通过网页所加载的文档确定应用程序缓存的状态</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> oAppCache = <span class="built_in">window</span>.applicationCache;</span><br><span class="line"><span class="keyword">var</span> sCacheStatus = <span class="string">"Not supported"</span>;</span><br><span class="line"><span class="keyword">switch</span> (oAppCache.status) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">0</span>: <span class="comment">// UNCACHED == 0</span></span><br><span class="line">        sCacheStatus = <span class="string">'（UNCACHED） : 无缓存， 即没有与页面相关的应用缓存'</span>;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">1</span>: <span class="comment">// IDLE == 1</span></span><br><span class="line">        sCacheStatus = <span class="string">'（IDLE） : 闲置，即应用缓存未得到更新'</span>;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">2</span>: <span class="comment">// CHECKING == 2</span></span><br><span class="line">        sCacheStatus = <span class="string">'（CHECKING） : 检查中，即正在下载描述文件并检查更新'</span>;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">3</span>: <span class="comment">// DOWNLOADING == 3</span></span><br><span class="line">        sCacheStatus = <span class="string">'（DOWNLOADING） : 下载中，即应用缓存正在下载描述文件'</span>;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">4</span>: <span class="comment">// UPDATEREADY == 4</span></span><br><span class="line">        sCacheStatus = <span class="string">'（UPDATEREADY） : 更新完成，所有资源都已下载完毕'</span>;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">5</span>: <span class="comment">// OBSOLETE == 5</span></span><br><span class="line">        sCacheStatus = <span class="string">'（IDLE） : 废弃，即应用缓存的描述文件已经不存在了，因此页面无法再访问应用缓存'</span>;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">        sCacheStatus = <span class="string">'（UKNOWN） ： CACHE STATUS '</span>;</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4-2-APPCACHE-事件"><a href="#4-2-APPCACHE-事件" class="headerlink" title="4.2 APPCACHE 事件"></a>4.2 APPCACHE 事件</h3><p>浏览器会对下载进度、应用缓存更新和错误状态等情况触发相应事件。</p>
<table>
<thead>
<tr>
<th>事件</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>checking</code></td>
<td>每当应用程序载入的时候，都会检查该清单文件,也总会首先触发<code>checking</code>事件。</td>
</tr>
<tr>
<td><code>noupdate</code></td>
<td>如果没有改动，同时应用程序也已经缓存了<code>noupdate</code>事件被触发，整个过程结束。</td>
</tr>
<tr>
<td><code>downloading</code></td>
<td>如果还未缓存应用程序，或者清单文件有改动,那么浏览器会下载并缓存清单中的所有资源 ,触发<code>downloading</code>事件，同时意味着下载过程开始。</td>
</tr>
<tr>
<td><code>progress</code></td>
<td>在下载过程中会间断性触发<code>progress</code>事件，通常是在每个文件下载完毕的时候。</td>
</tr>
<tr>
<td><code>cached</code></td>
<td>下载完成并且首次将应用程序下载到缓存中时，浏览器会触发<code>cached</code>事件。</td>
</tr>
<tr>
<td><code>updateready</code></td>
<td>当下载完成并将缓存中的应用程序更新后，浏览器会触发<code>updaterady</code>事件。</td>
</tr>
<tr>
<td><code>error</code></td>
<td>如果浏览器处于离线状态，检查清单列表失败，则会触发<code>error</code>事件，当一个未缓存的应用程序引用一个不存在的清单文件，也会触发此事件。</td>
</tr>
<tr>
<td><code>obsolete</code></td>
<td>如果一个缓存的应用程序引用一个不存在的清单文件，会触发<code>obsolete</code>，同时将应用从缓存中移除之后不会从缓存而是通过网络加载资。</td>
</tr>
</tbody>
</table>
<p>以下代码段为每种缓存事件类型设置了事件监听器：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">handleCacheEvent</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'对应操作'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">handleCacheError</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'Error: Cache failed to update!'</span>);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//在浏览器为应用缓存查找更新时触发</span></span><br><span class="line">oAppCache.addEventListener(<span class="string">'checking'</span>, handleCacheEvent, <span class="literal">false</span>);</span><br><span class="line"><span class="comment">//在检查描述文件发现文件无变化时触发</span></span><br><span class="line">oAppCache.addEventListener(<span class="string">'noupdate'</span>, handleCacheEvent, <span class="literal">false</span>);</span><br><span class="line"><span class="comment">// 在开始下载应用缓存资源时触发</span></span><br><span class="line">oAppCache.addEventListener(<span class="string">'downloading'</span>, handleCacheEvent, <span class="literal">false</span>);</span><br><span class="line"><span class="comment">//在文件下载应用缓存的过程中持续不断地下载地触发</span></span><br><span class="line">oAppCache.addEventListener(<span class="string">'progress'</span>, handleCacheEvent, <span class="literal">false</span>);</span><br><span class="line"><span class="comment">//在应用缓存完整可用时触发</span></span><br><span class="line">oAppCache.addEventListener(<span class="string">'cached'</span>, handleCacheEvent, <span class="literal">false</span>);</span><br><span class="line"><span class="comment">//在页面新的应用缓存下载完毕触发</span></span><br><span class="line">oAppCache.addEventListener(<span class="string">'updateready'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    oAppCache.swapCache();<span class="comment">// 更新本地缓存</span></span><br><span class="line">    location.reload();    <span class="comment">//重新加载页面页面</span></span><br><span class="line">&#125;, <span class="literal">false</span>);</span><br><span class="line"><span class="comment">//在检查更新或下载资源期间发送错误时触发</span></span><br><span class="line">oAppCache.addEventListener(<span class="string">'error'</span>, handleCacheError, <span class="literal">false</span>);</span><br><span class="line"><span class="comment">//缓存清单不存在时触发</span></span><br><span class="line">oAppCache.addEventListener(<span class="string">'obsolete'</span>, handleCacheEvent, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure>
<h2 id="5-更新缓存"><a href="#5-更新缓存" class="headerlink" title="5. 更新缓存"></a>5. 更新缓存</h2><p>一旦应用被缓存，它就会保持缓存直到发生下列情况：</p>
<ul>
<li>用户清空浏览器缓存</li>
<li>manifest 文件被修改</li>
<li>由程序来更新应用缓存</li>
</ul>
<p>以 “#” 开头的是注释行，但也可满足其他用途。应用的缓存会在其 manifest 文件更改时被更新。如果您编辑了一幅图片，或者修改了一个 JavaScript 函数，这些改变都不会被重新缓存。更新注释行中的日期和版本号是一种使浏览器重新缓存文件的办法。</p>
<p>由程序来更新应用缓存:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">oAppCache.addEventListener(<span class="string">'updateready'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">          oAppCache.swapCache();<span class="comment">// 更新本地缓存</span></span><br><span class="line">          location.reload();    <span class="comment">//重新加载页面页面</span></span><br><span class="line">&#125;, <span class="literal">false</span>);</span><br></pre></td></tr></table></figure>
<h2 id="6-注意事项"><a href="#6-注意事项" class="headerlink" title="6. 注意事项"></a>6. 注意事项</h2><ul>
<li>更新清单中列出的某个文件并不意味着浏览器会重新缓存该资源，清单文件本身必须进行更改。</li>
<li>浏览器对缓存数据的容量限制可能不太一样（某些浏览器设置的限制是每个站点 5MB）。</li>
<li>如果manifest文件，或者内部列举的某一个文件不能正常下载，整个更新过程都将失败，浏览器继续全部使用老的缓存。</li>
<li>引用 manifest 的 html 必须与 manifest 文件同源，在同一个域下。FALLBACK 中的资源必须和 manifest 文件同源。</li>
<li>浏览器会自动缓存引用 manifest 文件的 HTML 文件，这就导致如果改了 HTML 内容，也需要更新 manifest  文件版本或者由程序来更新应用缓存才能做到更新。</li>
<li>若遇到如此报错 <strong>“Application Cache Error event: Manifest fetch failed (404)”</strong> ,其原因是 manifest 文件需要正确的配置 MIME-type（描述该消息的媒体类型），即 <strong>“text/cache-manifest”</strong> ，必须在服务器端进行配置。不同服务器配置方式不一样。</li>
</ul>
<p>在tomcat服务器中的conf/web.xml中添加：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">mime-mapping</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">extension</span>&gt;</span>manifest<span class="tag">&lt;/<span class="name">extension</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mime-type</span>&gt;</span>text/cache-manifest<span class="tag">&lt;/<span class="name">mime-type</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mime-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="7-与传统浏览器缓存区别"><a href="#7-与传统浏览器缓存区别" class="headerlink" title="7. 与传统浏览器缓存区别"></a>7. 与传统浏览器缓存区别</h2><ul>
<li>离线缓存是针对整个应用，浏览器缓存是单个文件。</li>
<li>离线缓存断网了还是可以打开页面，浏览器缓存不行。</li>
<li>离线缓存可以主动通知浏览器更新资源。</li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/06/01/HTML5-WebWorker/" rel="next" title="HTML5 之 Web Worker">
                <i class="fa fa-chevron-left"></i> HTML5 之 Web Worker
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/06/01/JS-深浅拷贝/" rel="prev" title="JavaScript 之深浅拷贝™">
                JavaScript 之深浅拷贝™ <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/avatar.jpg"
               alt="曦瓜皮" />
          <p class="site-author-name" itemprop="name">曦瓜皮</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives/">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories/index.html">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        

      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-Application-Cache-简介"><span class="nav-number">1.</span> <span class="nav-text">1. Application Cache 简介</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-什么是-Application-Cache"><span class="nav-number">1.1.</span> <span class="nav-text">1.1. 什么是 Application Cache</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-应用缓存原理"><span class="nav-number">1.2.</span> <span class="nav-text">1.2. 应用缓存原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-使用方法"><span class="nav-number">1.3.</span> <span class="nav-text">1.3. 使用方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-浏览器兼容性"><span class="nav-number">1.4.</span> <span class="nav-text">1.4. 浏览器兼容性</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-Manifest-文件"><span class="nav-number">2.</span> <span class="nav-text">2. Manifest 文件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-简单实例"><span class="nav-number">3.</span> <span class="nav-text">3. 简单实例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-window-applicationCache"><span class="nav-number">4.</span> <span class="nav-text">4. window.applicationCache</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-status-属性值"><span class="nav-number">4.1.</span> <span class="nav-text">4.1. status 属性值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-APPCACHE-事件"><span class="nav-number">4.2.</span> <span class="nav-text">4.2 APPCACHE 事件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-更新缓存"><span class="nav-number">5.</span> <span class="nav-text">5. 更新缓存</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-注意事项"><span class="nav-number">6.</span> <span class="nav-text">6. 注意事项</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-与传统浏览器缓存区别"><span class="nav-number">7.</span> <span class="nav-text">7. 与传统浏览器缓存区别</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">曦瓜皮</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.2"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  






  





  

  

  

  

  

  

</body>
</html>
